<template>
  <div class="app-container">
    <div >
      <el-row :gutter="5" >
        <el-col :span="4" >
          <!--      最大边框   -->
          <div style="width:300px;height:600px;background-color: #FFFFFF; border-radius: 20px;margin: 0px">
            <!--        搜索框   -->
            <div class="head-container" style="padding-top: 10px;margin-bottom: 12px" >
              <el-input
                v-model="factoryName"
                placeholder="请输入部门名称"
                clearable
                size="small"
                prefix-icon="el-icon-search"
                style="
               width: 200px !important;
               margin-bottom: 10px !important;
               "
              />

              <!--             <hr style="width: 280px; opacity: 0.5;"/>-->

            </div>

            <div class="head-container" align="leftY">
              <el-tree
                :data="deptOptions"
                :props="defaultProps"
                :expand-on-click-node="false"
                :filter-node-method="filterNode"
                ref="tree"
                node-key="id"
                default-expand-all
                highlight-current
                @node-click="handleNodeClick"
              />
            </div>
          </div>
        </el-col>

        <el-col :span="16" >
          <div style="width:1200px;height:200px;margin-left: 10px" >
            <!--   生产信息   -->
            <el-col :span="20" :xs="24" >
              <!--展示数据-->
              <div id="formBok" style="background-color: #FFFFFF;width: 1200px; border-radius: 20px">
                <el-container>
                  <!--            头部-->
                  <el-header >
                    <el-row :gutter="5" style="padding-top: 15px">
                      <!--           左     -->
                      <el-col :span="8" align="left">
                        <el-row :gutter="10" class="mb8">
                          <el-col :span="1.5">
                            <el-button
                              type="primary"
                              plain
                              icon="el-icon-plus"
                              size="mini"
                              @click="handleAdd"
                              v-hasPermi="['jbxx:gcdy:add']"
                            >新增</el-button>
                          </el-col>
                          <el-col :span="1.5">
                            <el-button
                              type="success"
                              plain
                              icon="el-icon-edit"
                              size="mini"
                              :disabled="single"
                              @click="handleUpdate"
                              v-hasPermi="['jbxx:gcdy:edit']"
                            >编辑</el-button>
                          </el-col>
                          <el-col :span="1.5">
                            <el-button
                              type="danger"
                              plain
                              icon="el-icon-delete"
                              size="mini"
                              :disabled="multiple"
                              @click="handleDelete"
                              v-hasPermi="['jbxx:gcdy:remove']"
                            >删除</el-button>
                          </el-col>
                          <el-col :span="1.5">
                            <el-button
                              type="warning"
                              plain
                              icon="el-icon-download"
                              size="mini"
                              @click="handleExport"
                              v-hasPermi="['jbxx:gcdy:export']"
                            >导出</el-button>
                          </el-col>
                        </el-row>
                      </el-col>
                      <!--           右     -->
                      <el-col :span="16" align="right" >
                        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
                        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

                          <el-form-item  prop="isStart">
                            <el-select v-model="queryParams.isStart" placeholder="按启用的状态筛选" clearable>
                              <el-option value="1" label="正常" key="1"></el-option>
                              <el-option value="0" label="停用" key="0"></el-option>
                            </el-select>
                          </el-form-item>

                          <el-form-item  prop="dyName">
                            <el-input
                              v-model="queryParams.dyName"
                              placeholder="请输入生产单元名称"
                              clearable
                              @keyup.enter.native="handleQuery"
                            />
                          </el-form-item>

                          <el-form-item>
                            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                          </el-form-item>
                        </el-form>
                      </el-col>
                    </el-row>
                  </el-header>
                  <!--            中部-->
                  <el-main>
                    <el-table border  stripe v-loading="loading" :data="gcdyList" @selection-change="handleSelectionChange">
                      <el-table-column type="selection" width="55" align="center" />
                      <el-table-column label="序号" align="center" type="index" :index="indexMethod" />
                      <el-table-column label="生产单元编码" align="center" prop="dyCode" />
                      <el-table-column label="生产单元名称" align="center" prop="dyName" />
                      <el-table-column label="生产单元类型" align="center" prop="dyTypeCode" />
                      <el-table-column label="父级生产单元" align="center" prop="parent_dy_name" />
                      <el-table-column label="负责人" align="center" prop="fzrCode" />

                      <el-table-column label="启用" align="center" key="isStart" v-if="columns[0].visible">
                        <template slot-scope="scope">
                          <el-switch
                            v-model="scope.row.isStart"
                            active-value="true"
                            inactive-value="false"
                            @change="handleStatusChange(scope.row)"
                          ></el-switch>
                        </template>
                      </el-table-column>


                      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                        <template slot-scope="scope">
                          <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                            v-hasPermi="['jbxx:gcdy:edit']"
                          >修改</el-button>
                          <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-delete"
                            @click="handleDelete(scope.row)"
                            v-hasPermi="['jbxx:gcdy:remove']"
                          >删除</el-button>
                        </template>
                      </el-table-column>
                    </el-table>
                  </el-main>
                  <!--            底部-->
                  <el-footer>

                    <el-row :gutter="5">
                      <el-col :span="24" align="right">
                        <pagination
                          v-show="total>0"
                          :total="total"
                          :page.sync="queryParams.pageNum"
                          :limit.sync="queryParams.pageSize"
                          @pagination="getList"
                        />
                      </el-col>
                    </el-row>
                  </el-footer>
                </el-container>

                <!-- 添加或修改工厂定义对话框 -->
                <el-dialog  :title="title" :visible.sync="open" width="600px"  append-to-body>
                  <el-form ref="form" :model="form" :rules="rules"  >

                    <el-row :gutter="10">
                      <el-col :span="12">
                        <el-form-item label="生产单元名称：" prop="dyName" >
                          <el-input v-model="form.dyName" placeholder="请输入名称" style="width: 200px" />
                        </el-form-item>
                      </el-col>

                      <el-col :span="12"  align="left" >
                        <el-form-item label="生产单元类型："  prop="dyTypeCode">
                          <el-select v-model="form.dyTypeCode" :required="form.dyTypeCode !== '' && form.dyTypeCode !== 0" style="width: 200px">
                            <el-option value="请选择" label="请选择" key="0"></el-option>
                            <el-option value="工厂" label="工厂" key="1"></el-option>
                            <el-option value="车间" label="车间" key="2"></el-option>
                            <el-option value="产线" label="产线" key="3"></el-option>
                            <el-option value="工作中心" label="工作中心" key="4"></el-option>
                          </el-select>
                        </el-form-item>
                      </el-col>
                    </el-row>

                    <el-row :gutter="15">
                      <el-col :span="12">
                        <el-form-item label="父级生产单元：" prop="dyParentCode">
                          <el-input @click.native="openFactory" v-model="form.dyParentCode" placeholder="请选择" style="width: 200px"/>
                        </el-form-item>
                      </el-col>

                      <el-col :span="12" >
                        <el-form-item label="负责人：" prop="fzrCode" align="left">
                          <el-input @click.native="openHead" v-model="form.fzrCode" placeholder="请选择" style="width: 200px"/>
                        </el-form-item>
                      </el-col>
                    </el-row>

                  </el-form>

                  <div slot="footer" class="dialog-footer" align="center" style="padding-top: 150px">
                    <el-button @click="cancel">取 消</el-button>
                    <el-button type="primary" @click="submitForm">保 存</el-button>
                  </div>
                </el-dialog>


                <el-dialog title="选择生产单元" :visible.sync="dialogTableVisible">

                  <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

                    <el-row>
                      <el-col :span="8">
                        <div>
                          <el-form-item  prop="dyCode">
                            <el-input
                              v-model="queryParams.dyCode"
                              placeholder="关键字筛选"
                              clearable
                              @keyup.enter.native="handleQuery"
                            />
                          </el-form-item>
                        </div>
                      </el-col>

                      <el-col :span="16">
                        <div align="right">
                          <el-form-item>
                            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" style="background-color: rgba(0,176,192);color: #FFFFFF">查询</el-button>
                          </el-form-item>
                        </div>
                      </el-col>
                    </el-row>

                  </el-form>

                  <el-table border stripe :data="gridData" @selection-change="handleSelectionChangeFactory">
                    <el-table-column type="selection" width="55" align="center"  />
                    <el-table-column property="dyCode" label="生产单元编码" width="150"></el-table-column>
                    <el-table-column property="dyName" label="生产单元名称" width="200"></el-table-column>
                    <el-table-column property="dyTypeCode" label="生产单元类型"></el-table-column>
                    <el-table-column property="fzrCode" label="负责人"></el-table-column>
                  </el-table>

                  <el-row :gutter="5">
                    <el-col :span="24" align="right">
                      <pagination
                        v-show="total>0"
                        :total="total"
                        :page.sync="queryParams.pageNum"
                        :limit.sync="queryParams.pageSize"
                        @pagination="getList"
                      />
                    </el-col>
                  </el-row>

                  <div slot="footer" class="dialog-footer" align="center" style="padding-top: 20px">
                    <el-button @click="cancelFactory" style="background-color: rgba(0,176,192);color: #FFFFFF">取 消</el-button>
                    <el-button type="primary" @click="submitFormFactory">保 存</el-button>
                  </div>
                </el-dialog>


                <el-dialog title="选择人员" :visible.sync="dialogTableVisibleHead">

                  <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

                    <el-row>
                      <el-col :span="8">
                        <div>
                          <el-form-item  prop="dyCode">
                            <el-input
                              v-model="queryParams.dyCode"
                              placeholder="关键字筛选"
                              clearable
                              @keyup.enter.native="handleQuery"
                            />
                          </el-form-item>
                        </div>
                      </el-col>

                      <el-col :span="16">
                        <div align="right">
                          <el-form-item>
                            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" style="background-color: rgba(0,176,192);color: #FFFFFF">查询</el-button>
                          </el-form-item>
                        </div>
                      </el-col>
                    </el-row>

                  </el-form>

                  <el-table border stripe :data="userData" @selection-change="handleSelectionChangeHead">
                    <el-table-column type="selection" width="55" align="center"  />
                    <el-table-column property="userName" label="用户名" width="150"></el-table-column>
                    <el-table-column property="nickName" label="姓名" width="200"></el-table-column>
                    <el-table-column property="dept.deptName" label="所属部门"></el-table-column>
                  </el-table>

                  <el-row :gutter="5">
                    <el-col :span="24" align="right">
                      <pagination
                        v-show="total>0"
                        :total="total"
                        :page.sync="queryParams.pageNum"
                        :limit.sync="queryParams.pageSize"
                        @pagination="getList"
                      />
                    </el-col>
                  </el-row>

                  <div slot="footer" class="dialog-footer" align="center" style="padding-top: 20px">
                    <el-button @click="cancelHead" style="background-color: rgba(0,176,192);color: #FFFFFF">取 消</el-button>
                    <el-button type="primary" @click="submitFormHead">保 存</el-button>
                  </div>
                </el-dialog>

              </div>
            </el-col>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import { listGcdy,listGcdyBy, getGcdy, delGcdy, addGcdy, updateGcdy,updateGcdyIsStart,gcdyTreeSelect} from "@/api/jbxx/gcdy";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {getUserProfile, listUser} from "@/api/system/user";


export default {
  name: "Gcdy",
  dicts: ['sys_normal_disable'],
  components: { Treeselect , getUserProfile},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      user: {},
      roleGroup: {},
      postGroup: {},
      activeTab: "userinfo",
      // 部门树选项
      deptOptions: undefined,
      // 工厂名称
      factoryName: undefined,
      // 总条数
      total: 0,
      // 工厂定义表格数据
      gcdyList: [],
      index:1,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dyCode: undefined,
        dyName: undefined,
        dyTypeCode: undefined,
        dyParentCode: undefined,
        fzrCode: undefined,
        dyCreate: undefined,
        isStart: undefined,
        parent_dy_name:undefined,
        zhdy_name:undefined
      },
      // 表单参数
      form: {},
      dyCreate:undefined,
      // 表单校验
      rules: {
        dyName: [
          { required: true, message: "生产单元名称不能为空", trigger: "blur" }
        ],
        dyTypeCode: [
          { required: true, message: "请选择", trigger: "blur" }
        ],
        fzrCode: [
          { required: true, message: "负责人编号不能为空", trigger: "blur" }
        ],
        dyParentCode:[
          { required: true, message: "父级单元不能为空", trigger: "blur" }
        ]
      },
      // 树形控件
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      // 父级表格数据
      gridData: [],
      //负责人表格数据
      userData:[],
      dialogTableVisible: false,
      dialogTableVisibleHead:false,
      // 列信息
      columns: [
        { key: 0, label: `启用`, visible: true },
      ],
    };
  },
  watch: {
    // 根据名称筛选部门树
    factoryName(val) {
      console.log(this.$refs.tree.filter(val))
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getDeptTree();
    this.getListUser();
    this.getListFactory();
  },
  methods: {
    /** 查询工厂定义列表 */
    getList() {
      this.loading = true;
      listGcdy(this.queryParams).then(response => {

        for (let i = 0; i < response.rows.length; i++) {
          response.rows[i].isStart = response.rows[i].isStart === 1 ? 'true' : 'false';
        }
        this.gcdyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询用户列表 */
    getListUser() {
      listUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.userData = response.rows;
          this.total = response.total;
        }
      );
    },

    /** 查询父级单元列表 */
    getListFactory() {
      listGcdy(this.queryParams).then(response => {

        this.gridData = response.rows;
        this.total = response.total;

      });
    },
    /** 查询工厂定义列表 */
    getByList() {
      this.loading = true;
      listGcdyBy(this.queryParams).then(response => {

        for (let i = 0; i < response.rows.length; i++) {
          response.rows[i].isStart = response.rows[i].isStart === 1 ? 'true' : 'false';
        }
        this.gcdyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /**取消按钮*/
    cancel() {
      this.open = false;
      this.reset();
    },
    /**父级取消*/
    cancelFactory() {
      this.dialogTableVisible=false;
      this.open = true;
    },
    /**负责人取消*/
    cancelHead() {
      this.dialogTableVisibleHead=false;
      this.open = true;
    },
    /**表单重置*/
    reset() {
      this.form = {
        dyCode: undefined,
        dyName: undefined,
        dyTypeCode: undefined,
        dyParentCode: undefined,
        fzrCode: undefined,
        dyCreate: undefined,
        dyCreatetime: undefined,
        dyUpdatetime: undefined,
        isStart: undefined,
        isDelete: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getByList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dyCode)
      this.single = selection.length!==1
      this.multiple = !selection.length

    },

    /** 父级多选框选中数据 */
    handleSelectionChangeFactory(selection) {
      this.ids = selection.map(item => item.name)
      this.single = selection.length!==1
      this.multiple = !selection.length

      for (let i = 0; i < selection.length; i++) {
        this.form.dyParentCode = selection[i].dyName;
      }

    },

    /** 负责人多选框选中数据 */
    handleSelectionChangeHead(selection) {
      this.ids = selection.map(item => item.name)
      this.single = selection.length!==1
      this.multiple = !selection.length

      for (let i = 0; i < selection.length; i++) {
        this.form.fzrCode = selection[i].nickName;
      }

    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增生产单元";

    },

    /** 父级单元选择操作 */
    openFactory(){
      this.open = false;
      this.dialogTableVisible = true;
    },

    /** 负责人单元选择操作 */
    openHead(){
      this.open = false;
      this.dialogTableVisibleHead = true;
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const dyCode = row.dyCode || this.ids
      getGcdy(dyCode).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "编辑工厂定义";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {

        for (let i = 0; i < this.gcdyList.length; i++) {
          if (this.gcdyList[i].dyName == this.form.dyParentCode){
            this.form.dyParentCode = this.gcdyList[i].dyCode;
          }
        }

        if (valid) {
          if (this.form.dyCode != null) {
            updateGcdy(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.getListFactory();
            });
          } else {
            addGcdy(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.getListFactory();
            });
          }
        }
      });
    },

    /** 父级单元提交 */
    submitFormFactory(){
      this.dialogTableVisible = false;
      this.open = true;
    },

    /** 负责人单元提交 */
    submitFormHead(){
      this.dialogTableVisibleHead = false;
      this.open = true;
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const dyCodes = row.dyCode || this.ids;
      this.$modal.confirm('是否确认删除工厂定义编号为"' + dyCodes + '"的数据项？').then(function () {
        return delGcdy(dyCodes);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('jbxx/gcdy/export', {
        ...this.queryParams
      }, `gcdy_${new Date().getTime()}.xlsx`)
    },

    /** 节点单击事件 */
    handleNodeClick(data) {
      this.queryParams.dyCode = data.id;
      console.log(data.id)
      this.handleQuery();
    },

    /** 筛选节点 */
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },

    /** 查询部门下拉树结构 */
    getDeptTree() {
      gcdyTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },

    /** 用户状态修改 */
    handleStatusChange(row) {

      let text = row.isStart === "true" ? "启用" : "停用";

      let isStart = row.isStart === "true" ? "1" : "0";
      this.$modal.confirm('确认要"' + text + '""' + row.dyName + '"用户吗？').then(function()
      {

        return updateGcdyIsStart(row.dyCode, isStart);

      }).then(() => {

        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {

        row.isStart = row.isStart === "true" ? "1" : "0";
      });
    },

    /** 序号 */
    indexMethod(index) {
      return index + 1 ;
    },


  }
};
</script>
<style>
/*弹出框圆角边框样式*/
/*.el-dialog{
  border-radius: 20px;
}*/
</style>

